//
//  setCityViewController.m
//  weather
//
//  Created by zd2011 on 12-11-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "setCityViewController.h"

@interface setCityViewController ()

@end

@implementation setCityViewController
@synthesize table;
@synthesize delegate;
@synthesize cityList;
@synthesize allCityName;
@synthesize Dict;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置背景
    UIImageView *tableBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"city"]];
    [self.table setBackgroundView:tableBg];

    //这里是测试
    NSString *path=[[NSBundle mainBundle]pathForResource:@"city2" ofType:@"plist"];
    NSDictionary *dict=[[NSDictionary alloc]initWithContentsOfFile:path];
    NSLog(@"%d",[[dict allKeys]count ]);
    self.allCityName=[[dict allKeys]sortedArrayUsingSelector:@selector(compare:)];//排序
    NSLog(@"%d",[self.allCityName count]);
    self.Dict=dict;
        
    
    
}

- (void)viewDidUnload
{
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source
//表里有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return [self.allCityName count];
}
//一个分区里有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSString *key=[self.allCityName objectAtIndex:section];
    self.cityList=[self.Dict objectForKey:key];
    return [self.cityList count];
}
//每行的显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    //设置cell选中的背景色
    cell.selectionStyle=UITableViewCellSelectionStyleGray ;
    
    NSUInteger row=[indexPath row];
    NSUInteger section=[indexPath section];
    NSString *key=[self.allCityName objectAtIndex:section];
    self.cityList=[self.Dict objectForKey:key];
    cell.textLabel.text=[self.cityList objectAtIndex:row];
    
    
    
    cell.textLabel.textColor=[UIColor whiteColor];
    return cell;
}
//设置分区的标题值
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *key=[self.allCityName objectAtIndex:section];
    return key;
}
//索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.allCityName;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    UINavigationController *nav=self.navigationController;
    [nav popViewControllerAnimated:YES];
    
    NSArray *allControllers=nav.viewControllers;
    
        //NSLog(@"%@",nav.viewControllers);
    //获取导航控制器的视图
    todayViewController *today=[allControllers lastObject];
    NSInteger row=[indexPath row];
    NSInteger section=[indexPath section];
    NSString *key=[self.allCityName objectAtIndex:section];
    self.cityList=[self.Dict objectForKey:key];
    NSString *city=[self.cityList objectAtIndex:row];
    today.cityname=city;
    [today reload];
    
    
    
}
@end
